package com.sunnada.edu.business.salaryManage.service;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.stereotype.Service;

import com.sunnada.edu.system.pub.common.AppContext;
import com.sunnada.edu.system.pub.dao.PubDao;
import com.sunnada.edu.system.pub.util.Utils;

/**
 * 
 * @author Session Zhang
 * @created 2016年3月7日 下午3:54:22
 */
@Service
public class GetCurrMonthSalaryMethod  {
	/**
	 * 所有工人
	 * 
	 * @param params
	 * @return
	 */
	public static List getWorkerList(HashMap<String, String> params) {
		PubDao dao = AppContext.getInstance().getPubDao();
		String sql = " select T.REC_ID,T.NAME,T.SEX,T.TEL,T.NATION,T.IDCARD,T.NATIVE,T.EDUDEGREE,T.WORKTYPE,T.ADDR,T.ENTRYTIME, "
				+ " T.BANKCARD,T.JOBNO,T.PAPERCONTRACT,T.PHOTO,T.GROUPID,T.REC_STATUS,T.REC_TIME,T.REC_UPDATE, "
				+ " T.REC_UPDATEPERSION,T.REC_PERSION,T.AGE,T.REGISTEREDAGE,T2.SALARY, " + " T2.CHARGETYPE, " + " T2.UNITPAY, T2.GWSUBSIDY, T2.WCSUBSIDY, " + " T2.QTSUBSIDY, "
				+ " T2.YLSAFE,T2.SYSAFE, " + " T2.YWSAFE, T2.QTSAFE, T2.GJJ,T2.GS, T2.PAYFORM " + " FROM T_WORKER T " + " LEFT JOIN t_worker_salary_conf t2 on t.REC_ID=t2.REC_ID "
				+ " WHERE T.REC_STATUS!='2' order by t.rec_time asc";
		return dao.selectList(sql);
	}

	/**
	 * 添加某月工资记录
	 * 
	 * @param params
	 * @return
	 */
	public static int salaryRecAdd(HashMap<String, String> params) {
		PubDao dao = AppContext.getInstance().getPubDao();
		String recId = params.get("recId");
		String workerId = params.get("workerId");
		String salary = params.get("SALARY");
		String chargetype = params.get("CHARGETYPE");
		String unitpay = params.get("UNITPAY");
		String gwsubsidy = params.get("GWSUBSIDY");
		String wcsubsidy = params.get("WCSUBSIDY");
		String qtsubsidy = params.get("QTSUBSIDY");
		String ylsafe = params.get("YLSAFE");
		String sysafe = params.get("SYSAFE");
		String ywsafe = params.get("YWSAFE");
		String qtsafe = params.get("QTSAFE");
		String gjj = params.get("GJJ");
		String gs = params.get("GS");
		String payform = params.get("PAYFORM");
		/*****************************************************************/
		String workHours = params.get("workHours");
		String workQuantity = params.get("workQuantity");
		String workReward = params.get("workReward");
		String workSubsidy = params.get("workSubsidy");
		String attDeduction = params.get("attDeduction");
		String otherDeduction = params.get("otherDeduction");
		String wxyj = params.get("wxyj");
		String shouldGive = params.get("shouldGive");
		String realGive = params.get("realGive");
		String noGive = params.get("noGive");
		String recStatus = params.get("recStatus");
		String recTime = params.get("recTime");
		String sqladd = "";
		sqladd += " insert into t_worker_salary (REC_ID, WORKER_ID, SALARY, ";
		sqladd += " CHARGETYPE, UNITPAY, GWSUBSIDY, ";
		sqladd += " WCSUBSIDY, QTSUBSIDY, YLSAFE, ";
		sqladd += " SYSAFE, YWSAFE, QTSAFE, ";
		sqladd += " GJJ, GS, PAYFORM, WORK_HOURS, ";
		sqladd += " WORK_QUANTITY, WORK_REWARD, WORK_SUBSIDY, ";
		sqladd += " ATT_DEDUCTION, OTHER_DEDUCTION, WXYJ, ";
		sqladd += " SHOULD_GIVE, REAL_GIVE, NO_GIVE, ";
		sqladd += " REC_STATUS, REC_TIME) ";
		sqladd += " values(";
		sqladd += "'" + recId + "','" + workerId + "','" + salary + "','" + chargetype + "','" + unitpay + "','" + gwsubsidy + "','" + wcsubsidy + "','" + qtsubsidy + "','"
				+ ylsafe + "','" + sysafe + "',";
		sqladd += "'" + ywsafe + "','" + qtsafe + "','" + gjj + "','" + gs + "','" + payform + "','" + workHours + "','" + workQuantity + "','" + workReward + "','" + workSubsidy
				+ "','" + attDeduction + "',";
		sqladd += "'" + otherDeduction + "','" + wxyj + "','" + shouldGive + "','" + realGive + "','" + noGive + "','" + recStatus + "','" + recTime + "'";
		sqladd += ")";

		return dao.insert(sqladd);
	}

	/**
	 * 删除统计记录
	 * 
	 * @param params
	 * @return
	 */
	public static int salaryRecDel(HashMap<String, String> params) {
		PubDao dao = AppContext.getInstance().getPubDao();
		String workerId = params.get("workerId");
		String recTime = params.get("recTime");
		String sqladd = "";
		sqladd = "delete from t_worker_salary where worker_id='" + workerId + "' and rec_time='" + recTime + "'";
		return dao.insert(sqladd);
	}

	/**
	 * 
	 * 获取某个工人本月考勤总工时
	 * 
	 * @param params
	 * @return
	 */
	public static String getWorkHours(HashMap<String, String> params) {
		PubDao dao = AppContext.getInstance().getPubDao();
		String workerId = params.get("JOBNO");
		String recTime = params.get("recTime");
		//如果一天内有多次打卡，只要有一次未打卡，则总工时为0，回头进行考勤异常申请来弥补
		String sql = "SELECT if(SUM(WORKING_HOURS) is null,0,SUM(WORKING_HOURS)) WORKING_HOURS  FROM t_attendance_record t WHERE t.JOBNO='" + workerId + "' AND t.REC_TIME LIKE '"
				+ recTime + "%' AND NOT EXISTS(SELECT d.rec_id FROM t_attendance_record_data d WHERE  d.ATTENDANCE_STATUS='4' AND d.REC_ID=t.REC_ID) ";
		List<?> l1 = dao.selectList(sql);
		Double total = 0.0;// 本月总工时小时
		Double lateTotal = 0.0;// 本月迟到早退分钟
		Double rest = 0.0;// 剩余工时
		if (l1.size() > 0) {
			String WORKING_HOURS = ((Map) l1.get(0)).get("WORKING_HOURS") + "";
			total = Double.parseDouble(Utils.isNull(WORKING_HOURS) ? "0" : WORKING_HOURS);
			// 迟到早退
			sql = "SELECT if(sum(d.LATE_TIME) is null,0,sum(d.LATE_TIME)) LATE_TIME  FROM t_attendance_record t ,t_attendance_record_data d " + " WHERE t.JOBNO='" + workerId
					+ "' AND t.REC_TIME LIKE '" + recTime + "%' and t.REC_ID=d.REC_ID";
			List<?> l2 = dao.selectList(sql);
			if (l2.size() > 0) {
				String LATE_TIME = ((Map) l2.get(0)).get("LATE_TIME") + "";
				lateTotal = Double.parseDouble(Utils.isNull(LATE_TIME) ? "0" : LATE_TIME);
			}
		}
		//如果一天内有多次打卡，只要有一次未打卡，则总工时为0，但迟到早退的依然被算入，所以要补回去
		
		rest = total - lateTotal / 60;
		DecimalFormat df = new DecimalFormat("######0.00"); // 保留2位小数
		String restStr = df.format(rest);
		return restStr;
	}

	/**
	 * 
	 * 获取某个工人本月累计工作量
	 * 
	 * @param params
	 * @return
	 */
	public static String getWorkQuantity(HashMap<String, String> params) {
		PubDao dao = AppContext.getInstance().getPubDao();
		String workerId = params.get("workerId");
		String recTime = params.get("recTime");
		Double total = 0.0;
		String sql = "SELECT IF(SUM(t.PIECE_NUM) IS NULL,0,SUM(t.PIECE_NUM))  PIECE_NUM  FROM  t_piece_apply_worker t,t_process_result r "
				+ "WHERE r.SUBMIT_ID=t.apply_id AND r.REC_UPDATETIME LIKE '" + recTime + "%' AND t.worker_id='" + workerId + "' ";
		List<?> l = dao.selectList(sql);
		if (l.size() > 0) {
			String PIECE_NUM = ((Map) l.get(0)).get("PIECE_NUM") + "";
			total = Double.parseDouble(Utils.isNull(PIECE_NUM) ? "0" : PIECE_NUM);
		}
		DecimalFormat df = new DecimalFormat("###########.######"); // 去除小数
		String restStr = df.format(total);
		return restStr;
	}

	/**
	 * 
	 * 获取某个工人本月累计奖金
	 * 
	 * @param params
	 * @return
	 */
	public static  String getWorkReward(HashMap<String, String> params) {
		PubDao dao = AppContext.getInstance().getPubDao();
		String workerId = params.get("workerId");
		String recTime = params.get("recTime");
		Double total = 0.0;
		String sql = "SELECT IF(SUM(t.REWARD_MONEY) IS NULL,0,SUM(t.REWARD_MONEY))  REWARD_MONEY  FROM  t_reward_apply t,t_process_result r "
				+ "WHERE r.SUBMIT_ID=t.rec_id AND r.REC_UPDATETIME LIKE '" + recTime + "%' AND t.WORKER_ID='" + workerId + "' and t.REWARD_TYPE='1' ";
		List<?> l = dao.selectList(sql);
		if (l.size() > 0) {
			String REWARD_MONEY = ((Map) l.get(0)).get("REWARD_MONEY") + "";
			total = Double.parseDouble(Utils.isNull(REWARD_MONEY) ? "0" : REWARD_MONEY);
		}
		DecimalFormat df = new DecimalFormat("###########.######"); // 去除小数
		String restStr = df.format(total);
		return restStr;
	}

	/**
	 * 
	 * 获取某个工人本月累计补贴
	 * 
	 * @param params
	 * @return
	 */
	public static String getWorkSubsidy(HashMap<String, String> params) {
		Double GWSUBSIDY = Double.parseDouble(Utils.isNull(params.get("GWSUBSIDY")) ? "0" : params.get("GWSUBSIDY"));
		Double WCSUBSIDY = Double.parseDouble(Utils.isNull(params.get("WCSUBSIDY")) ? "0" : params.get("WCSUBSIDY"));
		Double QTSUBSIDY = Double.parseDouble(Utils.isNull(params.get("QTSUBSIDY")) ? "0" : params.get("QTSUBSIDY"));
		Double total = GWSUBSIDY + WCSUBSIDY + QTSUBSIDY;
		DecimalFormat df = new DecimalFormat("##################0.00"); // 保留两位小数
		String restStr = df.format(total);
		return restStr;
	}

	/**
	 * 
	 * 获取某个工人本月累计考勤扣款
	 * 
	 * @param params
	 * @return
	 */
	public static String getWorkAttDeduction(HashMap<String, String> params) {
		PubDao dao = AppContext.getInstance().getPubDao();
		String workerId = params.get("JOBNO");
		String recTime = params.get("recTime");
		Double total = 0.0;
		String sql = " SELECT IF(SUM(d.AMOUNT) IS NULL,0,SUM(d.AMOUNT)) AMOUNT  FROM t_attendance_record t ,t_attendance_record_data d " + " WHERE t.JOBNO='" + workerId
				+ "' AND t.REC_TIME LIKE '" + recTime + "%' AND t.REC_ID=d.REC_ID";
		List<?> l2 = dao.selectList(sql);
		if (l2.size() > 0) {
			String AMOUNT = ((Map) l2.get(0)).get("AMOUNT") + "";
			total = Double.parseDouble(Utils.isNull(AMOUNT) ? "0" : AMOUNT);
		}

		DecimalFormat df = new DecimalFormat("######0.00"); // 保留2位小数
		String restStr = df.format(total);
		return restStr;
	}

	/**
	 * 
	 * 获取某个工人本月累计其他扣款
	 * 
	 * @param params
	 * @return
	 */
	public static String getWorkOtherDeduction(HashMap<String, String> params) {
		PubDao dao = AppContext.getInstance().getPubDao();
		String workerId = params.get("workerId");
		String recTime = params.get("recTime");
		Double total = 0.0;
		String sql = "SELECT IF(SUM(t.REWARD_MONEY) IS NULL,0,SUM(t.REWARD_MONEY))  REWARD_MONEY  FROM  t_reward_apply t,t_process_result r "
				+ "WHERE r.SUBMIT_ID=t.rec_id AND r.REC_UPDATETIME LIKE '" + recTime + "%' AND t.WORKER_ID='" + workerId + "' and t.REWARD_TYPE!='1' ";
		List<?> l = dao.selectList(sql);
		if (l.size() > 0) {
			String REWARD_MONEY = ((Map) l.get(0)).get("REWARD_MONEY") + "";
			total = Double.parseDouble(Utils.isNull(REWARD_MONEY) ? "0" : REWARD_MONEY);
		}
		DecimalFormat df = new DecimalFormat("###########.######"); // 去除小数
		String restStr = df.format(total);
		return restStr;
	}
	
	/**
	 * 
	 * 获取某个工人本月累计请假扣款
	 * 
	 * @param params
	 * @return
	 */
	public static String getWorkAskForLeave(HashMap<String, String> params) {
		PubDao dao = AppContext.getInstance().getPubDao();
		String workerId = params.get("workerId");
		String recTime = params.get("recTime");
		Double total = 0.0;
		String sql = "SELECT IF(SUM(t.REWARD_MONEY) IS NULL,0,SUM(t.REWARD_MONEY))  REWARD_MONEY  FROM  t_reward_apply t,t_process_result r "
				+ "WHERE r.SUBMIT_ID=t.rec_id AND r.REC_UPDATETIME LIKE '" + recTime + "%' AND t.WORKER_ID='" + workerId + "' and t.REWARD_TYPE!='1' ";
		List<?> l = dao.selectList(sql);
		if (l.size() > 0) {
			String REWARD_MONEY = ((Map) l.get(0)).get("REWARD_MONEY") + "";
			total = Double.parseDouble(Utils.isNull(REWARD_MONEY) ? "0" : REWARD_MONEY);
		}
		DecimalFormat df = new DecimalFormat("###########.######"); // 去除小数
		String restStr = df.format(total);
		return restStr;
	}

	/**
	 * 
	 * 获取某个工人本月五险一金
	 * 
	 * @param params
	 * @return
	 */
	public static String getWorkWxyj(HashMap<String, String> params) {
		Double YLSAFE = Double.parseDouble(Utils.isNull(params.get("YLSAFE")) ? "0" : params.get("YLSAFE"));
		Double SYSAFE = Double.parseDouble(Utils.isNull(params.get("SYSAFE")) ? "0" : params.get("SYSAFE"));
		Double YWSAFE = Double.parseDouble(Utils.isNull(params.get("YWSAFE")) ? "0" : params.get("YWSAFE"));
		Double QTSAFE = Double.parseDouble(Utils.isNull(params.get("QTSAFE")) ? "0" : params.get("QTSAFE"));
		Double GJJ = Double.parseDouble(Utils.isNull(params.get("GJJ")) ? "0" : params.get("GJJ"));
		Double total = YLSAFE + SYSAFE + YWSAFE + QTSAFE + GJJ;
		DecimalFormat df = new DecimalFormat("##################0.00"); // 保留两位小数
		String restStr = df.format(total);
		return restStr;
	}

	/**
	 * 
	 * 获取某个工人本月应发
	 * 
	 * @param params
	 * @return
	 */
	public static String getWorkShouldGived(HashMap<String, String> params) {
		// 应发=基本工资+（工时、件数X单位薪酬） +奖励+补贴-考勤扣款-其他扣款-五险一金-个税-请假扣款
		// 基本工资
		Double SALARY = Double.parseDouble(Utils.isNull(params.get("SALARY")) ? "0" : params.get("SALARY"));
		// （工时、件数X单位薪酬）
		Double unitPayCount = 0.0;
		String chargeType = params.get("CHARGETYPE");
		Double UNITPAY = Double.parseDouble(Utils.isNull(params.get("UNITPAY")) ? "0" : params.get("UNITPAY"));
		Double workHours = Double.parseDouble(Utils.isNull(params.get("workHours")) ? "0" : params.get("workHours"));
		Double workQuantity = Double.parseDouble(Utils.isNull(params.get("workQuantity")) ? "0" : params.get("workQuantity"));
		if (Utils.isNotNull(chargeType)) {
			if ("1".equals(chargeType)) {// 按工程量
				unitPayCount = (workHours / 24) * UNITPAY;
			} else if ("2".equals(chargeType)) {// 按小时
				unitPayCount = workHours * UNITPAY;
			} else if ("3".equals(chargeType)) {// 按件
				unitPayCount = workQuantity * UNITPAY;
			}
		}
		// 奖励
		Double workReward = Double.parseDouble(Utils.isNull(params.get("workReward")) ? "0" : params.get("workReward"));
		// 补贴
		Double workSubsidy = Double.parseDouble(Utils.isNull(params.get("workSubsidy")) ? "0" : params.get("workSubsidy"));
		// 考勤扣款
		Double attDeduction = Double.parseDouble(Utils.isNull(params.get("attDeduction")) ? "0" : params.get("attDeduction"));
		// 其他扣款
		Double otherDeduction = Double.parseDouble(Utils.isNull(params.get("otherDeduction")) ? "0" : params.get("otherDeduction"));
		// 五险一金
		Double wxyj = Double.parseDouble(Utils.isNull(params.get("wxyj")) ? "0" : params.get("wxyj"));
		// 个税
		Double GS = Double.parseDouble(Utils.isNull(params.get("GS")) ? "0" : params.get("GS"));
		//请假扣款
		String askForLeave=getWorkAskForLeave(params);
		Double QJ = Double.parseDouble(Utils.isNull(askForLeave) ? "0" : askForLeave);
		
		Double total = SALARY + unitPayCount + workReward + workSubsidy - attDeduction - otherDeduction - wxyj - GS-QJ;
		DecimalFormat df = new DecimalFormat("##################0.00"); // 保留两位小数
		String restStr = df.format(total);
		return restStr;
	}

	/**
	 * 
	 * 获取某个工人本月实发=基本工资
	 * 
	 * @param params
	 * @return
	 */
	public static String getWorkRealGived(HashMap<String, String> params) {
		Double SALARY = Double.parseDouble(Utils.isNull(params.get("SALARY")) ? "0" : params.get("SALARY"));
		Double total = SALARY;
		DecimalFormat df = new DecimalFormat("##################0.00"); // 保留两位小数
		String restStr = df.format(total);
		return restStr;
	}

	/**
	 * 
	 * 获取某个工人本月未发
	 * 
	 * @param params
	 * @return
	 */
	public static String getWorkNoGived(HashMap<String, String> params) {
		Double SALARY = Double.parseDouble(Utils.isNull(params.get("SALARY")) ? "0" : params.get("SALARY"));
		Double shouldGive = Double.parseDouble(Utils.isNull(params.get("shouldGive")) ? "0" : params.get("shouldGive"));
		Double total = shouldGive-SALARY;
		DecimalFormat df = new DecimalFormat("##################0.00"); // 保留两位小数
		String restStr = df.format(total);
		return restStr;
	}

	public  void main(String[] args) {
		Double a = 2.333333333333333333333333;
		DecimalFormat decimalFormat = new DecimalFormat("###################0.00");
		System.out.println(decimalFormat.format(a));

	}

}
